<template>
  <div class="box">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>商家登录</span>
          <el-link @click="$router.push('/empLogin')">管理员登录</el-link>
        </div>
      </template>
      <el-form ref="loginForm" style="max-width: 600px" :model="business" :rules="rules" label-width="auto"
        status-icon>
        <el-form-item label="商家账号" prop="account">
          <el-input v-model="business.account" />
        </el-form-item>

        <el-form-item label="商家密码" prop="password">
          <el-input v-model="business.password" @keydown.enter="loginBusiness"/>
        </el-form-item>

      </el-form>
      <template #footer>
        <el-space  spacer="|">
          <el-button type="primary" @click="loginBusiness">登录</el-button>
          <el-button type="primary" @click="resetBusiness">重置</el-button>
          <el-link @click="$router.push('/register')">跳转到注册页面</el-link>
        </el-space>

      </template>
    </el-card>
  </div>
</template>

<script>
import {businessLogin} from '@/api/business.js'

import { ElMessage } from 'element-plus'

export default {
  data() {

    return {
      business: {},
      rules: {
        account: { required: true, message: '账号不能为空', trigger: 'blur' },
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 10, message: '密码位数在6到10之间', trigger: 'blur' }]
      }
    }
  },
  methods: {
    loginBusiness() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 发起异步请求,实现注册功能
          businessLogin(this.business).then((response)=> {
              if(response.data == 0){
                ElMessage.error('用户名或者密码不能为空')
              }else if(response.data == 1){
                ElMessage.error('用户名或者密码不存在')
              }else{
                //存储登录人信息到sessionstorage
                sessionStorage.setItem("businessInfo",JSON.stringify(response.data))
                //跳转到欢迎页面
                this.$router.push("/");
              }
            });
        }
      })
    },
    resetBusiness() {
      this.$refs.loginForm.resetFields();
    }
  }
}
</script>

<style scoped>

.card-header{
  display: flex;
  justify-content: space-between;
}

.box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  /* background-image: url(https://ppui-static-pc.cdn.bcebos.com/static/passpc-account/img/reg_bg_min.jpg); */
}

.el-card {
  width: 500px;
  height: 380px;
}
</style>